package org.example.movie;

import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;

import java.io.File;

/**
 * 图形模糊度 —— 直接视频流的截图分析
 *
 * @author zehua
 * @date 2023/11/7 14:27
 */
public class BlurDetectionPicture {

    public static void main(String[] args) throws Exception {
        File file = new File("D:\\work\\zehuasoftwore\\frame_5760.jpg"); // 模糊 frame_7530.jpg
        clarityException(file);
//        File fileFace = new File("D:\\work\\zehuasoftwore\\frame390_src.jpg"); // 模糊 // 14.223907868151162
//        clarityException(fileFace);
    }

    /**
     * javacv 检测图片清晰度
     * 标准差越大说明图像质量越好
     */
    public static void clarityException(File jpegFile){
        String path = "E:\\test\\";
        Mat srcImage = opencv_imgcodecs.imread(jpegFile.getAbsolutePath());
        Mat dstImage = new Mat();
        //转化为灰度图
        opencv_imgproc.cvtColor(srcImage, dstImage, opencv_imgproc.COLOR_BGR2GRAY);
        //在gray目录下生成灰度图片
        opencv_imgcodecs.imwrite(path+"gray-"+jpegFile.getName(), dstImage);

        Mat laplacianDstImage = new Mat();
        //阈值太低会导致正常图片被误断为模糊图片，阈值太高会导致模糊图片被误判为正常图片
        opencv_imgproc.Laplacian(dstImage, laplacianDstImage, opencv_core.CV_64F);
        //在laplacian目录下升成经过拉普拉斯掩模做卷积运算的图片
        opencv_imgcodecs.imwrite(path+"laplacian-"+jpegFile.getName(), laplacianDstImage);

        //矩阵标准差
        Mat stddev = new Mat();
        //求矩阵的均值与标准差
        opencv_core.meanStdDev(laplacianDstImage, new Mat(), stddev);
        // ((全部元素的平方)的和)的平方根
        // double norm = Core.norm(laplacianDstImage);
        // System.out.println("\n矩阵的均值：\n" + mean.dump());
        System.out.println(jpegFile.getName() + "矩阵的标准差：\n" + stddev.createIndexer().getDouble());
        // System.out.println(jpegFile.getName()+"平方根：\n" + norm);
    }
}
